//
//  KVNSlideView.m
//  Test1
//
//  Created by KelvinYeung on 15/7/16.
//  Copyright (c) 2015年 KelvinYeung. All rights reserved.
//

#import "KVNSlideView.h"

@interface KVNSlideView()

@property UIPageControl *pageControl;
@property UIScrollView  *scrollView;

@property (nonatomic)  long pageCount;//共多少页
@property int currentPage; //当前页
@property float pageWidth; //页宽
@property float pageHeight; //页高
@property NSArray * slideItems;//




@end

@implementation KVNSlideView
@synthesize scrollView;
@synthesize pageControl;
@synthesize currentPage;
@synthesize pageCount;
@synthesize data;

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype) init{
    self=[super init];
    
    return self;
}


- (instancetype) initWithFrame:(CGRect)aRect{
    self = [super initWithFrame:aRect];
    
    
    pageCount = 0;
    currentPage = 0;
    
    //初始化UISrollView
    scrollView = [[UIScrollView alloc] init];
    scrollView.frame = aRect;
    scrollView.alwaysBounceVertical = NO;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    //scrollView.backgroundColor= [UIColor colorWithPatternImage:[UIImage imageNamed:@"home_logo"]];
    scrollView.delegate = self;
    
    //初始化UIPageControll
    pageControl = [[UIPageControl alloc]init];
    pageControl.frame = CGRectMake(0, aRect.size.height+15, aRect.size.width, 10);
    
    [pageControl addTarget:self action:@selector(switchPage:) forControlEvents:UIControlEventValueChanged];
    //使用NSTimer实现定时触发滚动控件滚动的动作。
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
    
    [self addSubview:scrollView];
    [self addSubview:pageControl];
    
    //创建点击事件
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self addGestureRecognizer:singleFingerOne];
    
    return self;
}


- (void) setData:(NSArray *)aData{
    data= aData;
    
    //删除所有子视图
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    pageCount=aData.count;

    pageControl.numberOfPages = pageCount;
    pageControl.currentPage = 0;
    scrollView.contentSize = CGSizeMake(self.frame.size.width * pageCount, self.frame.size.height);
    
    int i = 0;
    for(NSDictionary *banner in aData){
       
        //用一个Button包起来
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height)];
        //NSLog(@"i is :%d",i);
        //imageView.backgroundColor = [UIColor yellowColor];
        
        if([banner isKindOfClass:[NSManagedObject class]]){
            [imageView setImageFromURL:[NSURL URLWithString:[banner valueForKey:@"bannerImage"]] placeHolderImage:[UIImage imageNamed:@"cell_loading"] animation:YES];
        }else{
            [imageView setImageFromURL:[NSURL URLWithString:[banner objectForKey:@"bannerImage"]] placeHolderImage:[UIImage imageNamed:@"cell_loading"] animation:YES];//[[UIImage alloc]initWithContentsOfFile:[banner objectForKey:@"bannerImage"]];
        
        }
        
        [self.scrollView addSubview:imageView];
        
        i++;
    }

    [self reloadInputViews];


}


#pragma mark - UIView点击事件
- (void) handleSingleFingerEvent:(UITapGestureRecognizer *) sender{
         //判断当前第几页；
    
     NSDictionary * dic = [data objectAtIndex:currentPage];
    NSLog(@"dic is :%@",data);
    _slideViewItemClick(dic);
    

    
}




//轮播图自动r播放
- (void) switchPage:(UIPageControl *) pageController{
    
    int whichPage = (int)pageController.currentPage;
    
    [UIView beginAnimations:nil context:NULL];
    
    [UIView setAnimationDuration:0.3f];
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    [self.scrollView setContentOffset:CGPointMake(320.0f * whichPage, 0.0f) animated:YES];
    
    [UIView commitAnimations];
    
}

//定时滚动

-(void)scrollTimer{
    
    currentPage++;
    
    if(currentPage == pageCount) {
        currentPage = 0;
    }
    
    self.pageControl.currentPage=currentPage;
    [self.scrollView scrollRectToVisible:CGRectMake(currentPage * [self superview].frame.size.width, 0, [self superview].frame.size.width, [self superview].frame.size.height) animated:YES];
}



#pragma mark - 滚动停止后改变currentPage的值
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    currentPage = (self.scrollView.contentOffset.x
                     / self.scrollView.frame.size.width);
    pageControl.currentPage = currentPage;
}

@end
